package com.iflytek.web.sevlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Random;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.iflytek.domain.User;
import com.iflytek.service.UserService;
import com.iflytek.utils.AutoCreateIdUtils;
import com.iflytek.utils.MD5Utils;
import com.iflytek.utils.MailUtils;
import com.iflytek.utils.SmsSDKUtils;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/register")
public class RegisterServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public RegisterServlet() {
		super();
	}

	// 获取表单中所有内容
	public void getForm(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		User user = new User();
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService us=(UserService) ac.getBean("userService");
		
		try {
			BeanUtils.populate(user, request.getParameterMap());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		String password=user.getPassword();
		String Md5Password=MD5Utils.md5(password);
		user.setUid(AutoCreateIdUtils.getUUID());
		user.setPassword(Md5Password);
		boolean isRegisterSuccess = us.findUserIsRegistSuccess(user);
		String message = "";
		String username = request.getParameter("username");
		if (isRegisterSuccess) {
			// if(request.getParameterMap().get("sex").clone().equals("男")) {
			// message+="您好"+username+"先生，恭喜您成功注册安信大酒店会员，欢迎您来本店预定。如有疑问可在线咨询"
			// + "我们定当为您提供优良的服务，谢谢";
			// }else
			// if(request.getParameterMap().get("sex").clone().equals("女")) {
			// message+="您好"+username+"女士，恭喜您成功注册安信大酒店会员，欢迎您来本店预定。如有疑问可在线咨询"
			// + "我们定当为您提供优良的服务，谢谢";
			// }
			// try {
			// MailUtils.sendMail(user.getUseremail(), message);
			// } catch (AddressException e) {
			// e.printStackTrace();
			// } catch (MessagingException e) {
			// e.printStackTrace();
			// }
			response.sendRedirect(request.getContextPath() + "/login.jsp");
		} else {
			response.sendRedirect(request.getContextPath() + "/register.jsp");
		}

	}

	// 发送验证码
	public void sendMessage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String phone = request.getParameter("userphone");
		String code = getCode(request);
		SmsSDKUtils.sendMessageCode(phone, code);
		response.getWriter().write("true");
		response.flushBuffer();
	}

	// 判断验证是否一样
	public void verification(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String code1 = (String) request.getSession().getAttribute("code");
		boolean isExist = true;
		if (code != null && code1 != null) {
			if (request.getSession().getAttribute("code").equals(code)) {
				isExist = true;
			} else {
				isExist = false;
			}
		} else {
			isExist = false;
		}
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
	}

	// 查询数据库中是否存在该手机号码
	public void isMobile(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ServletContext sc = request.getServletContext();
		WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);
		UserService us=(UserService) ac.getBean("userService");
		
		String phone = request.getParameter("userphone");
		boolean isExist = us.findPhoneNumberIsRegisted(phone);

		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);

	}

	// 获取验证码
	public String getCode(HttpServletRequest request) {
		Random rand = new Random();
		String code = "";
		for (int i = 0; i < 6; i++) {
			code += rand.nextInt(10);
		}
		request.getSession().setAttribute("code", code);
		return code;
		// String code1 = "123456";
		// request.getSession().setAttribute("code", code1);
		// return code1;
	}

}
